﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DataAccess;


namespace GUI
{
    public partial class frmKhachHang : Form
    {
        public frmKhachHang()
        {
            InitializeComponent();
        }

        KhachDTO kDTO = null;
        Status TrangThai = Status.Xem;

        private void AnHienTxt(Boolean t)
        {
            txtHoTen.ReadOnly = t;
            txtDiaChi.ReadOnly = t;
            txtCMND.ReadOnly = t;
            cboLoaiKhach.Enabled = !t;
        }
        private void LoadData()
        {
            khachDTOBindingSource.DataSource = KhachBUS.SelectAll();
            khachDTODataGridView.Enabled = true;

            btnKhachMoi.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnLuu.Enabled = false;
        }
        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            LoadData();

            AnHienTxt(true);
            DataSet ds = SqlAccess.GetTable("loaikhach");
            cboLoaiKhach.DataSource = ds.Tables[0];
            cboLoaiKhach.DisplayMember = "tenloaikhach";
            cboLoaiKhach.ValueMember = "maloaikhach";
        }

        private void btnKhachMoi_Click(object sender, EventArgs e)
        {
            kDTO = khachDTOBindingSource.AddNew() as KhachDTO;
            TrangThai = Status.Them;

            cboLoaiKhach.SelectedIndex = -1;
            khachDTODataGridView.Enabled = false;
            btnKhachMoi.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            AnHienTxt(false);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            kDTO.MaLoaiKhach = cboLoaiKhach.SelectedValue.ToString();
            string strKQ = null;
            if (TrangThai == Status.Them)
            {
                strKQ = KhachBUS.Them(kDTO);
                if (strKQ == "True")
                {
                    kDTO = null;
                    TrangThai = Status.Xem;
                    LoadData();
                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }
            if (TrangThai == Status.Sua)
            {
                strKQ = KhachBUS.CapNhat(kDTO);
                if (strKQ == "True")
                {
                    kDTO = null;
                    TrangThai = Status.Xem;
                    LoadData();
                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }         
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            kDTO = khachDTOBindingSource.Current as KhachDTO;
            TrangThai = Status.Sua;

            khachDTODataGridView.Enabled = false;
            btnKhachMoi.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            AnHienTxt(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string strKQ = null;
            kDTO = khachDTOBindingSource.Current as KhachDTO;
            if (MessageBox.Show("Bạn có chắc xóa", "Thông tin", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                strKQ = KhachBUS.Xoa(kDTO);
                if (strKQ == "True")
                {
                    khachDTOBindingSource.RemoveCurrent();

                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult dlgHoiThoat;
            dlgHoiThoat = MessageBox.Show("Bạn có chắc thoát??", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dlgHoiThoat == DialogResult.Yes)
                this.Close();
        }
    }
}
